package com.example.Arithmetic.Arithmetic;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * 日期：2023/12/28
 * 时间：9:39
 * 描述：桶排序实现
 * 每个数放入一个桶内，桶内排序，整体排序
 */
public class BucketSort {
    public void sort(int[] ints) {
        List<Integer>[] list = new ArrayList[10];
//        初始化对象
        for (int i = 0; i < list.length; i++) {
            list[i] = new ArrayList();
        }
        for (int i : ints) {
            list[i / 10].add(i);
        }
        int k = 0;
        InsertionSorting insertionSorting = new InsertionSorting();
        for (List<Integer> l1 : list) {
            Collections.sort(l1);
            for (Integer ii : l1) {
                ints[k++] = ii;
            }
        }
    }

    @Test
    public void test() {
        int[] a = new int[1000000];
        int i = 0;

        Random random = new Random();
        while (i < 100) {
            int i1 = random.nextInt(100);
            a[i] = i1;
            i++;
        }
        int[] ages = {20, 10, 28, 66, 25, 31, 67, 30, 70};
        BucketSort bucketSort = new BucketSort();
        List<Integer>[] list = new ArrayList[10];
//        初始化对象
        for (int i1 = 0; i1 < list.length; i1++) {
            list[i1] = new ArrayList();
        }
        for (int i1 : ages) {
            list[i1 / 10].add(i1);
        }
        int k = 0;
        InsertionSorting insertionSorting = new InsertionSorting();
        for (List<Integer> l1 : list) {
            Collections.sort(l1);
            for (Integer ii : l1) {
                ages[k++] = ii;
            }
        }
        System.out.println(Arrays.toString(ages));
    }
}
